#include <stdlib.h>
#include "user_info_handler.h"

UserInfo *UserInfoHandler_NewUserInfo(int sockfd, char player_mode, uint8_t default_connect_expect)
{
    UserInfo *user_info = malloc(sizeof(UserInfo));
    
    user_info->sockfd = sockfd;
    user_info->player_mode = player_mode;
    user_info->connection_expect = default_connect_expect;
    
    pthread_mutex_init(&user_info->lock, NULL);

    return user_info;
}


void UserInfoHandler_DestoryUserInfo(UserInfo *user_info)
{
    pthread_mutex_destroy(&user_info->lock);
    free(user_info);
}


void UserInfoHandler_SetOpponentAddress(UserInfo *user_info, struct sockaddr_in opponent_address)
{
    pthread_mutex_lock(&user_info->lock);
    user_info->opponent_address = opponent_address;
    pthread_mutex_unlock(&user_info->lock);
}


void UserInfoHandler_SetConnectionExpect(UserInfo *user_info, uint8_t connection_expect)
{
    pthread_mutex_lock(&user_info->lock);
    user_info->connection_expect = connection_expect;
    pthread_mutex_unlock(&user_info->lock);
}


void UserInfoHandler_SetPlayerMode(UserInfo *user_info, char player_mode)
{
    user_info->player_mode = player_mode;
}


struct sockaddr_in UserInfoHandler_GetOpponentAddress(UserInfo *user_info)
{
    struct sockaddr_in opponent_address;

    pthread_mutex_lock(&user_info->lock);
    opponent_address = user_info->opponent_address;
    pthread_mutex_unlock(&user_info->lock);

    return opponent_address;
}


uint8_t UserInfoHandler_GetConnectionExpect(UserInfo *user_info)
{
    uint8_t connection_expect;

    pthread_mutex_lock(&user_info->lock);
    connection_expect = user_info->connection_expect;
    pthread_mutex_unlock(&user_info->lock);

    return connection_expect;
}


char UserInfoHandler_GetPlayerMode(UserInfo *user_info)
{
    return user_info->player_mode;
}